package com.jiuyou.clientController.mall.order;

import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.domain.MallOrderMesh;
import com.ruoyi.shopping.service.IMallOrderMeshService;
import com.ruoyi.shopping.service.IMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author chengliang
 * @date 2022/8/1
 **/
@RestController
@RequestMapping("/client/mall/order/option")
public class OrderOptionClientController extends ClientBaseController {

    @Autowired
    private IMallOrderService orderService;
    @Autowired
    private IMallOrderMeshService meshService;

    @RequestMapping("/applyRefund")
    public AjaxResult applyRefund(String cause, Long orderId) {
        MallOrder mallOrder = orderService.selectMallOrderById(orderId);
        if (mallOrder == null) {
            return AjaxResult.error("订单不存在");
        }
        mallOrder.setRefundStatus(DicConstants.REFUND_STATUS_Apply.getCode());
        mallOrder.setRefundReason(cause);
        mallOrder.setRefundMoney(mallOrder.getPayPrice());
        mallOrder.setRefundTime(new Date());
        mallOrder.setAfterSalesState(DicConstants.SYS_Y.getCode());
        orderService.updateMallOrder(mallOrder);
        MallOrderMesh mesh = meshService.selectMallOrderMeshById(mallOrder.getMergeOrderId());
        mesh.setAftersaleState(DicConstants.SYS_Y.getCode());
        meshService.updateMallOrderMesh(mesh);
        return AjaxResult.success();
    }

    @RequestMapping("/getOrderDetail/{id}")
    public AjaxResult getOrderDetail(@PathVariable Long id) {
        MallOrder mallOrder = orderService.selectMallOrderById(id);
//        mallOrder.setDeliverytype(DictUtils.getDictLabel("sys_deliver",));
        return AjaxResult.success(mallOrder);
    }

    @RequestMapping("/confirmShipped/{id}")
    public AjaxResult confirmShipped(@PathVariable Long id) {
        MallOrder mallOrder = orderService.selectMallOrderById(id);
        if (mallOrder == null) {
            return AjaxResult.error("订单不存在");
        }
        mallOrder.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
        orderService.updateMallOrder(mallOrder);
        List<MallOrder> mallOrders =
                orderService.selectMallOrderByMeshId(mallOrder.getMergeOrderId());
        int count = 0;
        for (MallOrder order : mallOrders) {
            if(DicConstants.ORDER_STATE_Received.getCode().equals(order.getOrderState())){
                count++;
            }
        }
        if(count == mallOrders.size()){
            MallOrderMesh mallOrderMesh = meshService.selectMallOrderMeshById(mallOrder.getMergeOrderId());
            mallOrderMesh.setShipState(DicConstants.SYS_Y.getCode());
            mallOrderMesh.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
            meshService.updateMallOrderMesh(mallOrderMesh);
        }
        return AjaxResult.success(mallOrder);
    }
    @RequestMapping("/confirmAll/{id}")
    public AjaxResult confirmAll(@PathVariable Long id) {

        List<MallOrder> mallOrders =
                orderService.selectMallOrderByMeshId(id);
        for (MallOrder order : mallOrders) {
            if(DicConstants.ORDER_STATE_Shipped.getCode().equals(order.getOrderState())){
                order.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
                orderService.updateMallOrder(order);
            }
        }
        MallOrderMesh mallOrderMesh = meshService.selectMallOrderMeshById(id);

        mallOrderMesh.setShipState(DicConstants.SYS_Y.getCode());
        mallOrderMesh.setOrderState(DicConstants.ORDER_STATE_Received.getCode());
        meshService.updateMallOrderMesh(mallOrderMesh);
        return AjaxResult.success();
    }
    @RequestMapping("/cancelOrder/{id}")
    public AjaxResult cancelOrder(@PathVariable Long id){
        meshService.cancelOrder(id);
        return AjaxResult.success();

    }


}
